/****************************************************************************************************
 *	Method trims, any field value sent to it, off space characters at the start and end 
 *	Input vars:	
 *		argvalue		The value (string) which is to be trimmed	
 *****************************************************************************************************/

function fnTrim(argvalue) {
	while (true) {
		if (argvalue.substring(0, 1) != " ") {
			break;
		}
		argvalue = argvalue.substring(1, argvalue.length);
	}
	while (true) {
		if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ") {
			break;
		}
		argvalue = argvalue.substring(0, argvalue.length - 1);
	}
	return argvalue;
}

/****************************************************************************************************
 *	This method will return all elements having a partiuclar ID
 *	Input vars:
 *		idVal		The ID of the tag which has to be searched
 *****************************************************************************************************/

function getElementsById(idVal) {
	if (document.getElementsByTagName('*')) {
		var elements = document.getElementsByTagName('*');
		var retElems = new Array();
		for ( var i = 0, j = 0; i < elements.length; i++) {
			if (elements[i].id == idVal) {
				retElems[j] = elements[i];
				j++;
			}
		}
		return retElems;
	} else
		return false;
}

/****************************************************************************************************
 *	This method will return all elements having a partiuclar name
 *	Input vars:
 *		idVal		The ID of the tag which has to be searched
 *****************************************************************************************************/

function getElementsByName(nameVal) {
	if (document.getElementsByTagName('*')) {
		var elements = document.getElementsByTagName('*');
		var retElems = new Array();
		for ( var i = 0, j = 0; i < elements.length; i++) {
			if (elements[i].name == nameVal) {
				retElems[j] = elements[i];
				j++;
			}
		}
		return retElems;
	} else
		return false;
}

/****************************************************************************************************
 *	This method will direct the user to the intended action
 *  path.
 *  Input vars:
 *  	action	=> the action mapping
 *  	meth	=> the method name
 *  	params	=> the parameters to be passed
 *****************************************************************************************************/
function fnForward(action, meth, params)
{	
	var url = '';
	var prefix = false;
	
	if(appContext)
	{
		url += appContext;
		if(action)
		{
			url += '/' + action;
		}
		else
		{
			alert("'action' not present!");
			return false;
		}
		if(meth)
		{
		   if (Form && Form.impl)
		   {
		      Form.impl.value = meth;
	//	      alert('Form has impl :'+Form.impl.value);
		   }
		   else
		   {
		      url += '?impl=' +meth;
		      prefix = true;
		   }
		}
		if(params)
		{
		   if (prefix)
		   {
		      url += '&' +params;
		   }
		   else
		   {
		      url += '?' +params;
		   }
		}
		Form.action = url;
	//	alert('forwarding to =>'+Form.action)
		
		setTimeout('Form.submit()', 50); 
	}
	else
	{
		alert("'appContext' has not been set!");
	}
}
	
/****************************************************************************************************
 * This method removes any page specific element's values.
 * NOTE: The ID's of the elements to be blanked needs to be passed. 
 *       No arguments constraints.
 * *************************************************************************************************/
function fnRemVars()
{
   for(var i=0; i< arguments.length; i++)
   {
      if(document.getElementById(arguments[i]))
         document.getElementById(arguments[i]).value = '';
   }
}

/****************************************************************************************************
 * This method toggles the display of any element block in a page.
 * Input vars:
 * NOTE: The ID's of the elements to be blanked needs to be passed. 
 *       No # of arguments constraints.
 * *************************************************************************************************/
function toggleBlockDisp()
{
   var arrElems = new Array();
   var arrActions = new Array();
   for(var i=0; i< arguments.length; i++)
   {
      if (i%2 == 0)
      {
         arrElems.push(arguments[i]);
      }
      else
      {
         arrActions.push(arguments[i]);
      }
   }
   if (arrElems.length != arrActions.length)
   {
      alert('Incorrect number of params.');
      return false;
   }
   for(var i=0; i< arrElems.length; i++)
   {
      if (document.getElementById(arrElems[i]))
      {
         document.getElementById(arrElems[i]).style.display = arrActions[i];
      }
   }
}

/****************************************************************************************************
 * This method toggles the visibility of any element block in a page.
 * Input vars:
 * NOTE: The ID's of the elements to be blanked needs to be passed. 
 *       No # of arguments constraints.
 * *************************************************************************************************/
function toggleBlockVis()
{
   var arrElems = new Array();
   var arrActions = new Array();
   for(var i=0; i< arguments.length; i++)
   {
      if (i%2 == 0)
      {
         arrElems.push(arguments[i]);
      }
      else
      {
         arrActions.push(arguments[i]);
      }
   }
   if (arrElems.length != arrActions.length)
   {
      alert('Incorrect number of params.');
      return false;
   }
   for(var i=0; i< arrElems.length; i++)
   {
      if (document.getElementById(arrElems[i]))
      {
         document.getElementById(arrElems[i]).style.visibility = arrActions[i];
      }
   }
}

/****************************************************************************************************
 * This method hadnles any AJAX call made to the server.
 * Input vars:
 *    url      => The URL to make the request to
 *    isSynch  => Is the call synchronous?
 *    callback => The callback method to be associated (can be null also)
 * *************************************************************************************************/

var ajaxHndlr = {
      container : [],
      cancel : function (ajaxObj) {
         if (ajaxObj && ajaxObj.readyState != 4 && ajaxObj.readyState != 0) {
            ajaxObj.onreadystatechange = function () {};
            ajaxObj.abort();
            ajaxObj = null;
          }
      }, 
      add : function (ajaxObj) {
         this.container.push(ajaxObj);
      },
      isCompleted : function() {
         if (this.container.length < 1) {
            return true;
         }
          for (var i = 0; i< this.container.length; i++) {
            var ajaxObj = this.container[i];
            if (ajaxObj && ajaxObj.readyState != 4 && ajaxObj.readyState != 0) {
               alert('There are pending requests. Please wait for them to complete.');
               return false;
            }
          }
         return true;
      },
      ajaxReqObj : {
         url : null,
         params : null,
         isASync : null,
         callback : null,
         req : null
      },
      makeCall : function(url, params, isASync, callback, isPost) 
      {
         //Check if there are any pending calls
         if (!this.isCompleted())
         {
            return false;
         }
         var reqParam;
         var reqType = (isPost) ? "POST" : "GET";
         ajaxReqObj = this.ajaxReqObj;
         ajaxReqObj.url = url;
         ajaxReqObj.isASync = isASync;
         ajaxReqObj.callback = callback;
         ajaxReqObj.params = params;
         ajaxReqObj.isPost = isPost;
         if (window.XMLHttpRequest) // Get req obj for a FF browser env
         {
            ajaxReqObj.req = new XMLHttpRequest();
         } 
         else if (window.ActiveXObject) // Get req obj for a IE browser env
         {
            ajaxReqObj.req = new ActiveXObject("Microsoft.XMLHTTP");
         }
         try
         {
            if (ajaxReqObj.callback)
            {
               ajaxReqObj.req.onreadystatechange = this.forward; 
            }
            if (ajaxReqObj.isPost)
            {
               reqParam = params;
            }
            else
            {
               ajaxReqObj.url = ajaxReqObj.url + '?' +params;
            }
            ajaxHndlr.add(ajaxReqObj.req); 
            ajaxReqObj.req.open(reqType, ajaxReqObj.url, ajaxReqObj.isASync);
            if (isPost)
            {
               ajaxReqObj.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
               ajaxReqObj.req.setRequestHeader("Content-length", reqParam.length);
               ajaxReqObj.req.setRequestHeader("Connection", "close");
            }
            ajaxReqObj.req.send(reqParam);
         } 
         catch (e)
         {
            alert('Mayday[' + e + ']: Could not make the AJAX request for =>[' + url
                  + ']');
         }
         return true;
      },
      getStatus : function() {return this.ajaxReqObj.req.status;},
      getState : function() {return this.ajaxReqObj.req.readyState;},
      getText : function() {return this.ajaxReqObj.req.responseText;},
      getXML : function() {return this.ajaxReqObj.req.responseXML;},
      forward : function() {
         if (ajaxHndlr.getState() == 4)
         {
            if (ajaxHndlr.getStatus() == 200)
            {
               ajaxReqObj.callback();
            }
            else
            {
               alert("Server's got a sleeper-jeeper!");
            }
         }
      }
   };

/****************************************************************************************************
 * This method handles the pressing of the 'Enter'/'Return' key on a particular
 * page.
 * Input vars:
 *    e     => The event generated
 *    objId => The ID of the element whose onclick has to be simulated
 *    exec  => Standard JS statement(s) which is/are to be executed
 * *************************************************************************************************/
   function fnChkEnter(e, objId, exec)
   {
      var srcElement = (e.srcElement) ? e.srcElement : e.target;
      
      if (e.keyCode == 13 && srcElement.tagName != 'TEXTAREA')
      {
         if (objId)
         {
            obj = document.getElementById(objId);
            callMeth = obj.onclick;
            if (typeof callMeth == 'function')
            {
               callMeth();
            }
         }
         if (exec)
         {
            try
            {
               eval(exec);
            }
            catch(ex)
            {
               alert("Command cannot be executed.");
            }
         }
         //Incase the check's being run from a nested element
         //then we don't want this event to bubble up and run the 
         //check on a different level as well.
         e.cancelBubble=true;
      }
   }
   
   /****************************************************************************************************
    * This method sets the values of form elements in bulk.
    * All elements who's values need to be set are sent as arguments to this 
    * method in the form of 'elementName', 'elementValToSet'
    * *************************************************************************************************/   
   function fnSetParams()
   {
      if (arguments)
      {  
         for (var i=0; i< arguments.length; i++)
         {
            if (Form[arguments[i]])
            {
               Form[arguments[i]].value = arguments[++i]
            }
            else if (document.getElementById(arguments[i]))
            {
               document.getElementById(arguments[i]).value = arguments[++i];
            }
         }
      }
   }